<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateSettingsTable extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $table = $this->table('settings', ['engine' => 'InnoDB', 'comment' => '系统设置表']);
        $table->addColumn('key', 'string', ['limit' => 50, 'null' => false, 'comment' => '设置键名'])
            ->addColumn('value', 'text', ['null' => false, 'comment' => '设置值'])
            ->addColumn('create_time', 'datetime', ['null' => false, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['null' => false, 'comment' => '更新时间'])
            ->addIndex(['key'], ['unique' => true])
            ->create();
            
        // 添加默认设置
        $rows = [
            [
                'key' => 'site_name',
                'value' => '隐形纸条系统',
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ],
            [
                'key' => 'commission_rates',
                'value' => json_encode([
                    'v1' => ['same' => 0.3, 'level1' => 0.1, 'level2' => 0.05],
                    'v2' => ['same' => 0.4, 'level1' => 0.15, 'level2' => 0.1, 'level3' => 0.05],
                    'v3' => ['same' => 0.5, 'level1' => 0.2, 'level2' => 0.15, 'level3' => 0.1],
                ]),
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ],
            [
                'key' => 'note_price',
                'value' => '1.00',
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ],
            [
                'key' => 'draw_price',
                'value' => json_encode([
                    'single' => '2.00',
                    'multi' => '5.00'
                ]),
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ],
            [
                'key' => 'upgrade_fee',
                'value' => json_encode([
                    'v1' => 50,
                    'v2' => 100,
                    'v3' => 200
                ]),
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ],
            [
                'key' => 'min_withdraw',
                'value' => '50.00',
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ],
            [
                'key' => 'withdraw_fee',
                'value' => '0.01',
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ],
        ];
        
        $this->table('settings')->insert($rows)->save();
    }
} 